//
//  ZFJWebViewController.m
//  ZFJiOSDemo
//
//  Created by ZFJMBPro on 2020/1/16.
//  Copyright © 2020 ZFJ. All rights reserved.
//

#import "ZFJWebViewController.h"
#import <WebKit/WebKit.h>
#import "ZFJWebCache.h"

@interface ZFJWebViewController ()

@property (nonatomic,strong) UIButton *rightBtn;
@property (nonatomic,strong) WKWebView *wkWebView;

@end

@implementation ZFJWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self uiconfig];
}

- (UIButton *)rightBtn{
    if(_rightBtn == nil){
        _rightBtn = [[UIButton alloc] init];
        _rightBtn.frame = CGRectMake(0, 0, 50, 30);
        [_rightBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_rightBtn setTitle:@"本地图片" forState:UIControlStateNormal];
        [_rightBtn setTitle:@"网络图片" forState:UIControlStateSelected];
        [_rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
        _rightBtn.selected = YES;
    }
    return _rightBtn;
}

- (void)uiconfig{
    // 设置缓存过期时间
    [ZFJCacheConfig instance].updateInterval = 1200;
    [ZFJCacheConfig instance].isLoadLocalResource = YES;
    
    NSArray *ignoreUrlArray = @[@"http://www.baidu.com"];
    [ZFJCacheConfig instance].ignoreUrlArray = ignoreUrlArray;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightBtn];
    
    WKWebView *wkWebView = [[WKWebView alloc] initWithFrame:self.view.bounds];
    wkWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    wkWebView.backgroundColor = [UIColor whiteColor];
//    wkWebView.navigationDelegate = self;
//    wkWebView.UIDelegate = self;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_urlStr]];
    [wkWebView loadRequest:request];
    [self.view addSubview:wkWebView];
    self.wkWebView = wkWebView;
}

- (void)rightBtnClick{
    self.rightBtn.selected = !self.rightBtn.selected;
    [ZFJCacheConfig instance].isLoadLocalResource = self.rightBtn.selected;

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_urlStr]];
    [self.wkWebView loadRequest:request];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //开始监听缓存数据
    [ZFJCacheProtocol startMonitorRequest:ZFJCacheAllType];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //在不需要用到webview的时候即使的取消监听
    [ZFJCacheProtocol cancelMonitorRequest];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
